package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.search.domain.DestinationEs;
import cn.wolfcode.wolf2w.search.domain.StrategyEs;
import cn.wolfcode.wolf2w.search.domain.TravelEs;
import cn.wolfcode.wolf2w.search.domain.UserInfoEs;
import cn.wolfcode.wolf2w.search.query.SearchQueryObject;
import cn.wolfcode.wolf2w.search.service.ISearchService;
import cn.wolfcode.wolf2w.search.vo.SearchResult;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.JsonResult;
import cn.wolfcode.wolf2w.util.ParamMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class SearchController {

    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private ITravelService travelService;
    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private ISearchService searchService;


    @GetMapping("/q")
    public Object search(SearchQueryObject qo) throws UnsupportedEncodingException {

        //编码处理
        qo.setKeyword(URLDecoder.decode(qo.getKeyword(), "UTF-8"));

        switch (qo.getType()){
            //目的地
            case SearchQueryObject.TYPE_DEST:
                return searchDest(qo);
            //攻略
            case SearchQueryObject.TYPE_STRATEGY:
                return searchStrategy(qo);
            //游记
            case SearchQueryObject.TYPE_TRAVEL:
                return searchTravel(qo);
            //用户
            case SearchQueryObject.TYPE_USER:
                return searchUserInfo(qo);
            //全部
            default:
                return searchAll(qo);
        }
    }

    //目的地
    private Object searchDest(SearchQueryObject qo) {
        //通过用户传入name查询对象
        Destination dest = destinationService.queryByName(qo.getKeyword());
        SearchResult result = new SearchResult();

        //判断用户输入条件是否为目的地
        if (dest !=null){
            List<Strategy> sts = strategyService.findByDestName(qo.getKeyword());

            List<Travel> ts = travelService.findByDestName(qo.getKeyword());
            List<UserInfo> us = userInfoService.findByCity(qo.getKeyword());
            long total = sts.size() + ts.size() + us.size();
            result.setTotal(total);
            result.setStrategys(sts);
            result.setTravels(ts);
            result.setUsers(us);
        }
        //dest
        //qo
        return JsonResult.success(new ParamMap()
                .put("qo", qo)
                .put("result", result)
                .put("dest", dest));
    }


    //攻略
    private Object searchStrategy(SearchQueryObject qo) {
        //通过页面输入关键词进行全文搜索，结果需要高亮显示
        Page<Strategy> page = searchService.searchWithHighlight(StrategyEs.INDEX_NAME,StrategyEs.TYPE_NAME,Strategy.class,
                qo,"title","subTitle","summary");

        Map<String, Object> map = new HashMap<>();
        map.put("page",page);
        map.put("qo",qo);

        return JsonResult.success(map);
    }

    //全部
    private Object searchAll(SearchQueryObject qo) {

        SearchResult vo = new SearchResult();
        long total = 0L;

        Page<Strategy> sts = searchService.searchWithHighlight(StrategyEs.INDEX_NAME,StrategyEs.TYPE_NAME,Strategy.class,
                qo,"title","subTitle","summary");
            vo.setStrategys(sts.getContent());
            total +=sts.getTotalElements();
        Page<Travel> ts = searchService.searchWithHighlight(TravelEs.INDEX_NAME,TravelEs.TYPE_NAME,Travel.class,
                qo,"title","summary");
            vo.setTravels(ts.getContent());
            total +=ts.getTotalElements();
        Page<UserInfo> us = searchService.searchWithHighlight(UserInfoEs.INDEX_NAME, UserInfoEs.TYPE_NAME,UserInfo.class,
                qo,"info","city");
            vo.setUsers(us.getContent());
            total +=us.getTotalElements();
        Page<Destination> ds = searchService.searchWithHighlight(DestinationEs.INDEX_NAME, DestinationEs.TYPE_NAME,Destination.class,
                qo,"info","name");
            vo.setDests(ds.getContent());
            total +=ds.getTotalElements();
            vo.setTotal(total);

        Map<String, Object> map = new HashMap<>();
        map.put("result",vo);
        map.put("qo",qo);

        return JsonResult.success(map);
    }

    //用户
    private Object searchUserInfo(SearchQueryObject qo) {
        Page<UserInfo> page = searchService.searchWithHighlight(UserInfoEs.INDEX_NAME, UserInfoEs.TYPE_NAME,UserInfo.class,
                qo,"info","city");

        Map<String, Object> map = new HashMap<>();
        map.put("page",page);
        map.put("qo",qo);

        return JsonResult.success(map);
    }
    //游记
    private Object searchTravel(SearchQueryObject qo) {
        Page<Travel> page = searchService.searchWithHighlight(TravelEs.INDEX_NAME,TravelEs.TYPE_NAME,Travel.class,
                qo,"title","summary");

        Map<String, Object> map = new HashMap<>();
        map.put("page",page);
        map.put("qo",qo);

        return JsonResult.success(map);
    }






}
